package com.yuhanginfo.modules.web.util;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.yuhanginfo.common.utils.ReadProperties;
import com.yuhanginfo.modules.web.entity.LLQCar;
import com.yuhanginfo.modules.web.entity.LLQCarBarand;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
/**
 * 007接口调用
 *
 */
public class CarInfo {
	private CarInfo(){}
	public static final Logger LOG =  LoggerFactory.getLogger(CarInfo.class);
	
	//获取4个品牌及其code
	public static List<LLQCarBarand> getCarBarandInfo(){  
		Map<String, String> str=new HashMap<String,String>();
		sign(str);
	 	String message=HttpPost.sendPost(ReadProperties.ReadByName("CAR_BARAND_URL"), str);
	 	JSONObject json = JSONObject.fromObject(message);
	 	List<LLQCarBarand> resultList=new ArrayList<LLQCarBarand>();
	 	if(json!=null) {
	 		List<LLQCarBarand> llqList = JSONArray.toList(handleResult(json), new LLQCarBarand(), new JsonConfig());//参数1为要转换的JSONArray数据，参数2为要转换的目标数据，即List盛装的数据
	        for(LLQCarBarand car:llqList) {
	        	if("audi".equals(car.getBrand())||"vwag".equals(car.getBrand())||"bmw".equals(car.getBrand())||"benz".equals(car.getBrand())) {
	        		resultList.add(car);
	        	}
	        }
	 	}
	 	return resultList;
	}
	
	public static LLQCar getCarInfoByVin(String carCode,String vin) {
		Map<String, String> str=new HashMap<String,String>();
		str.put("all",carCode);
		str.put("vin", vin);
		sign(str);
		String message=HttpPost.sendPost(ReadProperties.ReadByName("CAR_VIN"), str);
		JSONObject json = JSONObject.fromObject(message);
		String code=json.getString("code");
		LLQCar car =new LLQCar();
		if("1".equals(code)) {
			JSONObject data =json.getJSONObject("data");
			car.setBrand((String)data.get("brand"));
			car.setMains((String)data.get("mains"));
			car.setName((String)data.get("name"));
			car.setVin((String)data.get("vin"));
			car.setImglogo((String)data.get("imglogo"));
			car.setSub((String)data.get("sub"));
			car.setLfc_log((String)data.get("lfc_log"));
			car.setCode(code);
		}else {
			LOG.info("code:"+code+",msg:"+json.getString("msg"));
		}
		return car;
	}
	
	public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = Long.parseLong(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
	
	private static String byteHEX(byte ib) {  
	    char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
	    		'A', 'B', 'C', 'D', 'E', 'F' };  
	    char[] ob = new char[2];  
	    ob[0] = Digit[(ib >>> 4) & 0X0F];  
	    ob[1] = Digit[ib & 0X0F];  
	    return new String(ob); 
	} 
	
	private static void sign(Map<String, String> data) {
		long timestamp = System.currentTimeMillis() / 1000;
		data.put("time", Long.toString(timestamp));
		data.put("uid", ReadProperties.ReadByName("UID"));
		data.put("hashid", ReadProperties.ReadByName("HASH_ID"));
		Object[] keys = data.keySet().toArray();
		Arrays.sort(keys);
		StringBuilder sb = new StringBuilder();
		for(Object key: keys) {
			sb.append(data.get(key));
		}
		sb.append(ReadProperties.ReadByName("APP_KEY"));
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] rs = md.digest(sb.toString().getBytes("UTF-8")); 
			StringBuilder sbHex = new StringBuilder();  
			for (byte b: rs) {
				sbHex.append(byteHEX(b));
			}
			data.put("hash", sbHex.toString());
		} catch (Exception e) {
			LOG.error(e.getMessage(),e);
		}	
	}
	
	private static JSONArray handleResult(JSONObject json) {
		String code=json.getString("code");
		String msg=json.getString("msg");
		if("1".equals(code)&&"success".equals(msg)) {
			return json.getJSONArray("data");
		}else {
			LOG.info("code:"+code+",msg:"+msg);
			return null;
		}
	}
}
